package com.example.privacy;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.privacy.Utils.SharedPreferencesUtils;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class CreateOrder extends AppCompatActivity {

    private EditText etgoods;
    private EditText etrpeople;
    private EditText etrlocal;
    private Button createButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_order);
        etgoods = (EditText)findViewById(R.id.goods_name);
        etrpeople = (EditText)findViewById(R.id.input_rpeople);
        etrlocal = (EditText)findViewById(R.id.input_rlocal);
        createButton = (Button)findViewById(R.id.create_button);
        createButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doCreate();
            }
        });
    }

    private void doCreate(){
        String gooeds = etgoods.getText().toString();
        String people = etrpeople.getText().toString();
        String local = etrlocal.getText().toString();
        if(gooeds== null||gooeds.equals("")){
            Toast.makeText(this,"货物名不可为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if(people== null||people.equals("")){
            Toast.makeText(this,"货物名不可为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if(local== null||local.equals("")){
            Toast.makeText(this,"货物名不可为空",Toast.LENGTH_SHORT).show();
            return;
        }
        Create(gooeds,people,local);

    }

    private void Create(final String g, final String p, final String l){
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = getString(R.string.baseUrl)+"/CreateOrder";
                url = url + String.format("?goodname=%s&rpeople=%s&rlocal=%s", g,p,l);
                System.out.println(url);
                final OkHttpClient client = new OkHttpClient();
                RequestBody body = RequestBody.create(null,"");
                final Request request = new Request.Builder()
                        .url(url)
                        .addHeader("Authorization","Bearer "+ SharedPreferencesUtils.getInstance(CreateOrder.this).getString("token"))
                        .post(body)
                        .build();
                try {
                    //判断请求是否成功，也就是判断登录是否成功
                    Response response = client.newCall(request).execute();
                    if(response.code()==200){
                        String result = response.body().string();
                        String goodsuuid = "";
                        if(result.equals("货物名不可为空")) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(CreateOrder.this,"货物名不可为空",Toast.LENGTH_LONG).show();
                                }
                            });
                        }else if(result.equals("收货地址不可为空")) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(CreateOrder.this,"收货地址不可为空",Toast.LENGTH_LONG).show();
                                }
                            });
                        }else if(result.equals("收货人不可为空")) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(CreateOrder.this,"收货人不可为空",Toast.LENGTH_LONG).show();
                                }
                            });
                        }else if(result.split(" ")[0].equals("success")){
                            goodsuuid = result.split(" ")[4];
                            //带着uuid跳转，并结束当前创建页面
                            Intent i = new Intent(CreateOrder.this, ShowQRActivity.class);
                            i.putExtra("uuid",goodsuuid);
                            startActivity(i);
                        }
                        //完善成功，并跳转
                    }else{

                    }
                    response.body().close();
                }catch (Exception e){
                    System.out.println(e.getMessage().toString());
                }
            }
        }).start();
    }
}